package com.zhengdongzulin.zuke.app.main.apartment.sign

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.PayMethod
import com.zhengdongzulin.zuke.app.data.Roommate
import com.zhengdongzulin.zuke.app.data.SignData
import com.zhengdongzulin.zuke.app.main.mine.lifeservice.ZKLifeServiceCreateActivity
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse
import java.io.File

class SignVm : BaseViewModel() {

    //上传
    var upload: StateLiveData<ZKLifeServiceCreateActivity.FileData> = StateLiveData()
    fun upload(files: File, name: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postForm(Api.UPLOAD_SFZ)
                    .add("file", files)
                    .toResponse<ZKLifeServiceCreateActivity.FileData>()
                    .await()
                upload.postValueAndSuccess(r.apply {
                    this.name = name
                })
            }, {
                assignment(it, upload)
            }
        )
    }

    var get_sign_house_info: StateLiveData<SignData> = StateLiveData()

    fun get_sign_house_info(houseId: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.GET_SIGN_HOUSE_INFO)
                    .add("houseId", houseId)
                    .toResponse<SignData>()
                    .await()
                get_sign_house_info.postValueAndSuccess(r)
            }, {
                assignment(it, get_sign_house_info)
            }
        )
    }

    var get_sign_channel_source_dic_list: StateLiveData<MutableList<PayMethod>> = StateLiveData()

    fun get_sign_channel_source_dic_list() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.GET_SIGN_CHANNEL_SOURCE_DIC_LIST)
                    .toResponse<MutableList<PayMethod>>()
                    .await()
                get_sign_channel_source_dic_list.postValueAndSuccess(r)
            }, {
                assignment(it, get_sign_channel_source_dic_list)
            }
        )
    }

    var get_first_money: StateLiveData<SignData> = StateLiveData()

    fun get_first_money(houseId: String, payTypeId: String, rentTypeId: String, startDate: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.GET_FIRST_MONEY)
                    .add("houseId", houseId)
                    .add("payTypeId", payTypeId)
                    .add("rentTypeId", rentTypeId)
                    .add("startDate", startDate)
                    .toResponse<SignData>()
                    .await()
                get_first_money.postValueAndSuccess(r)
            }, {
                assignment(it, get_first_money)
            }
        )
    }

    var bill_details: StateLiveData<SignData> = StateLiveData()

    fun bill_details(houseId: String, payTypeId: String, rentTypeId: String, startDate: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.BILL_DETAILS)
                    .add("houseId", houseId)
                    .add("payTypeId", payTypeId)
                    .add("rentTypeId", rentTypeId)
                    .add("startDate", startDate)
                    .toResponse<SignData>()
                    .await()
                bill_details.postValueAndSuccess(r)
            }, {
                assignment(it, bill_details)
            }
        )
    }

    var sign_online: StateLiveData<SignData> = StateLiveData()

    fun sign_online(
        houseId: String,
        emergencyPeo: String,   //紧急联系人姓名
        emergencyPeoPhone: String,  //紧急联系人手机号
        payTypeId: String,  //支付方式字典id
        startDate: String,  //合同起租日
        endDate: String,    //合同截止日
        zukeSfzZheng: String,
        zukeSfzFan: String,
        channelSourceId: String,
        emergencyemail: String,
        emergencyhomeAddress: String,
        roommateList: MutableList<Roommate> //合租人信息
    ) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.SIGN_ONLINE)
                    .add("houseId", houseId)
                    .add("emergencyPeo", emergencyPeo)
                    .add("emergencyPeoPhone", emergencyPeoPhone)
                    .add("payTypeId", payTypeId)
                    .add("startDate", startDate)
                    .add("endDate", endDate)
                    .add("roommateList", roommateList)
                    .add("zukeSfzZheng", zukeSfzZheng)
                    .add("zukeSfzFan", zukeSfzFan)
                    .add("channelSourceId", channelSourceId)

                    .add("email", emergencyemail)
                    .add("homeAddress", emergencyhomeAddress)

                    .toResponse<SignData>()
                    .await()
                sign_online.postValueAndSuccess(r)
            }, {
                assignment(it, sign_online)
            }
        )
    }

}